using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using GameLib;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Controllers;

namespace TileEngine
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Level level;
        Texture2D levelBackground;
        Texture2D background;
        Texture2D rocky;
        Texture2D tree;

        Map map;
        const int screenWidth = 800;
        const int screenHeight = 600;        

        // Animating our player.
        CelAnimationManager cam;
        EnemyManager enemyManager;

        float scale = 0.70f;

        public PhysicsSimulator physicsSimulator;
        public PhysicsSimulatorView physicsSimulatorView;     

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            cam = new CelAnimationManager(this, @"Sprites\");
            //enemyManager = new EnemyManager(this);

            Components.Add(cam);
            //Components.Add(enemyManager);

            level = new Level(this, 1);
        }

        protected override void Initialize()
        {
            IsFixedTimeStep = true;
            TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 10);

            physicsSimulator = new PhysicsSimulator(new Vector2(0, 1100));
            physicsSimulatorView = new PhysicsSimulatorView(physicsSimulator);
            physicsSimulatorView.LoadContent(GraphicsDevice, Content);

            Components.Add(level);            
            base.Initialize();
        }

        protected override void LoadContent()
        {            
            spriteBatch = new SpriteBatch(GraphicsDevice);
                      
            MediaPlayer.Play(Content.Load<Song>(@"Music\song"));
            map = Content.Load<Map>(@"Maps\Map2");
            levelBackground = Content.Load<Texture2D>(@"desert background");
            background = Content.Load<Texture2D>(@"bg");
            rocky = Content.Load<Texture2D>(@"rocky");
            tree = Content.Load<Texture2D>(@"tree");

            level.SetMap(map);
            level.Load(ref physicsSimulator);
            
            //enemyManager.Load(spriteBatch, physicsSimulator);
            
            base.LoadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();
            if(keyboard.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            if (keyboard.IsKeyDown(Keys.Q))
            {
                scale += 0.01f;
            }
            if (keyboard.IsKeyDown(Keys.E))
            {
                scale -= 0.01f;
            }

            physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * 0.001f);
            base.Update(gameTime);
        }

        public Matrix getCamera()
        {
            Vector2 translation = new Vector2((level.player.body.Position.X) - (screenWidth / 2), (level.player.body.Position.Y) - (screenHeight - 165));
            return Matrix.CreateTranslation(new Vector3(-translation, 0)) * Matrix.CreateScale(scale);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            
            spriteBatch.Begin();
            spriteBatch.Draw(levelBackground, new Vector2(-150, 0), Color.White);

            // Debug info.
            spriteBatch.DrawString(Content.Load<SpriteFont>(@"Fonts\Arial"), "Body Position: " + level.player.position.ToString(), new Vector2(10, 10), Color.White);
            spriteBatch.DrawString(Content.Load<SpriteFont>(@"Fonts\Arial"), "Scale: " + scale.ToString(), new Vector2(10, 30), Color.White);
            
            spriteBatch.End();

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, getCamera());

            spriteBatch.Draw(rocky, new Vector2(134, 889), Color.White);
            spriteBatch.Draw(tree, new Vector2(750, 225), Color.White);
            spriteBatch.Draw(tree, new Vector2(1120, 550), Color.White);
            spriteBatch.Draw(rocky, new Vector2(1570, 590), Color.White);
            

            level.DrawTiles(spriteBatch);
            level.DrawPlayer(gameTime, spriteBatch);

            //physicsSimulatorView.Draw(spriteBatch);
            base.Draw(gameTime);
            spriteBatch.End();
        }
    }
}
